﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.XSchEmA.Entity;
using Pixysoft.Framework.XSchEmA.Aspect;

namespace Pixysoft.Framework.XSchEmA.Modifier
{

    class TableModifier : ITableModifier
    {
        ISchemaAspect aspect = null;

        public TableModifier(ISchemaAspect aspect)
        {
            this.aspect = aspect;
        }

        private string DEFAULT_COLUMNNAME = "COLUMN1";

        public TableSchema CreateTable(DatabaseSchema database, string tableName)
        {
            TableSchema table = new TableSchema(tableName);
            table.Columns.Add(new ColumnSchema(tableName, DEFAULT_COLUMNNAME, aspect.GetDefaultDataType()));
            table.BuildColumns();

            database.Tables.Add(table);
            table.Inspection();
            return table;
        }

        /// <summary>
        /// 外键问题
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="database">The database.</param>
        /// <returns></returns>
        public void DeleteTable(DatabaseSchema database, string tableName)
        {
            TableSchema table = database.GetTable(tableName);

            if (table == null)
                throw new Exception("missing tablename " + tableName);

            foreach (ForeignKeySchema fk in database.GetForeignKeys(table))
                database.ForeignKeys.Remove(fk);

            foreach (ForeignKeySchema fk in database.GetBeingForeignKeys(table))
                database.ForeignKeys.Remove(fk);

            PrimaryKeySchema pk = database.GetPrimaryKey(table);
            if (pk != null)
                database.PrimaryKeys.Remove(pk);

            List<UniqueSchema> uniquelist = database.GetUniques(table);
            foreach (UniqueSchema unique in uniquelist)
                database.Uniques.Remove(unique);

            List<IndexSchema> indexlist = database.GetIndexs(table);
            foreach (IndexSchema index in indexlist)
                database.Indexs.Remove(index);

            database.Tables.Remove(table);

            //09-11-07 build

            database.Build();
        }

        public void CreateTable(DatabaseSchema databaseTo, DatabaseSchema databaseFrom, TableSchema table)
        {
            if (!databaseTo.ContainedTable(table.TableName))
                databaseTo.Tables.Add(table);

            databaseTo.Build();

            foreach (IndexSchema index in databaseFrom.GetIndexs(table))
            {
                new ColumnModifier(aspect).CreateIndex(databaseTo, index);
            }

            foreach (UniqueSchema unique in databaseFrom.GetUniques(table))
            {
                new ColumnModifier(aspect).CreateUnique(databaseTo, unique);
            }

            PrimaryKeySchema pk = databaseFrom.GetPrimaryKey(table);
            if (pk != null)
            {
                new ColumnModifier(aspect).CreatePrimaryKey(databaseTo, pk);
            }
        }
    }
}
